/* 公共JS */

import $ from 'jquery'

// 时间戳转Date实例
function timeStampToDate(timeStamp) {
    return new Date(parseInt(timeStamp))
}

// Date实例转时间戳
function dateToTimeStampString(date) {
    return Date.parse(date).toString()
}

// 判断两个时间戳是否在同一天
function twoTimeStampIsSameDay(oneTimeStamp, twoTimeStamp) {
    return timeStampToDate(oneTimeStamp).toDateString() === timeStampToDate(twoTimeStamp).toDateString()
}

// 判断两个时间戳的HH:mm时间是否一致
function twoTimeStampHHmmIsSame(oneTimeStamp, twoTimeStamp) {
    const oneDate = timeStampToDate(oneTimeStamp)
    const twoDate = timeStampToDate(twoTimeStamp)
    return oneDate.getHours() === twoDate.getHours() && oneDate.getMinutes() === twoDate.getMinutes()
}

// 判断timeStamp是否在规定时间内
function isStartDateTimeStampBetweenEndDateTimeStamp(startDateTimeStamp, timeStamp, endDateTimeStamp) {
    return timeStamp >= startDateTimeStamp && timeStamp <= endDateTimeStamp
}

// 将时间戳格式化时间为HH:mm格式
function formatTimeStampToHHmm(timeStamp) {
    const date = timeStampToDate(timeStamp)
    return date.getHours().toString().padStart(2, '0') + ":" + date.getMinutes().toString().padStart(2, '0')
}

// 将时间戳格式化时间为YY-MM-DD格式
function formatTimeStampToYYMMDD(timeStamp) {
    const date = timeStampToDate(timeStamp)
    return `${date.getFullYear().toString()}.${(timeStampToDate(timeStamp).getMonth() + 1).toString().padStart(2, '0')}.${date.getDate().toString().padStart(2, '0')}`
}


/* 设置按钮点击动画 */

// 按钮样式恢复
function btnBlur(ele) {
    ele = $(ele)
    setTimeout(() => $(ele).removeClass("btnActive"), 50)
}

// 为所有.btnFocus的元素绑定动画事件
function btnFocusBind() {
    document.querySelectorAll(".btnFocus").forEach(ele => {
        ele.onmousedown = function () {
            $(this).addClass("btnActive")
        }

        ele.onmouseup = function () {
            btnBlur(this)
        }

        ele.ontouchstart = function () {
            $(this).addClass("btnActive")
        }

        ele.ontouchcancel = function () {
            btnBlur(this)
        }

        ele.ontouchend = function () {
            btnBlur(this)
        }
    })
}

// 按钮防抖
function bottomDisableOrAllow(ele, msg, disable = false) {
    ele = $(ele)
    ele.val(msg)
    ele.text(msg)
    ele.attr("disabled", disable)
    let isDisable = disable ? "opacity(50%)" : "none"
    ele.css("filter", isDisable)
}


// 图片压缩
function cutImage(file, max_size, quality = 1) {
    /*  图片压缩
        返回Promise，调用需要前缀await
        file: File对象
        max_size: 压缩后最大的字节数
        quality: 压缩后的图片质量，递归使用，无需传入
    */
    const canvas = document.createElement('canvas')
    const ctx = canvas.getContext('2d')
    return new Promise((resolve) => {
        const img = new Image()
        const url = window.URL || window.webkitURL
        img.src = url.createObjectURL(file)
        img.onload = function () {
            canvas.width = this.width
            canvas.height = this.height
            ctx.drawImage(img, 0, 0, this.width, this.height)
            canvas.toBlob((cutBlob) => {
                if (cutBlob.size > max_size && quality > 0) resolve(cutImage(new File([cutBlob], file.name.split(".").shift() + ".jpeg", {type: "image/jpeg"}), max_size, quality - 0.1))
                else resolve(new File([cutBlob], file.name.split(".").shift() + ".jpeg", {type: "image/jpeg"}))
            }, file.type, quality)
        }
    })
}


// 抛出
export default {
    timeStampToDate,
    dateToTimeStampString,
    btnFocusBind,
    twoTimeStampIsSameDay,
    twoTimeStampHHmmIsSame,
    formatTimeStampToYYMMDD,
    isStartDateTimeStampBetweenEndDateTimeStamp,
    formatTimeStampToHHmm,
    bottomDisableOrAllow,
    cutImage,
}
